package com.kefu.robot.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kefu.common.context.KefuContext;
import com.kefu.robot.model.RobotAidataInfo;
import com.kefu.robot.service.RobotAidataInfoService;
import com.kefu.sys.mapper.SysTenantMapper;
import com.kefu.sys.mapper.SysUserMapper;
import com.kefu.sys.model.SysTenant;
import com.kefu.sys.model.SysUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kefu.common.controller.AbstractBaseController;
import com.kefu.common.result.Result;
import com.kefu.common.result.ResultGenerator;
import com.kefu.common.vo.DetailVo;
import com.kefu.robot.vo.RobotAidataInfoSearchVo;
import com.kefu.robot.vo.RobotAidataInfoInsertVo;
import com.kefu.robot.vo.RobotAidataInfoUpdateVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author xiaoyu
 * @date 2023/11/03
 */
@RestController
@Api(tags = "AI模型信息")
@RequestMapping("robot/aidata/info")
public class RobotAidataInfoController extends AbstractBaseController {

    @Autowired
    private RobotAidataInfoService robotAidataInfoService;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysTenantMapper sysTenantMapper;

    @PostMapping("/add")
    @ApiOperation(value = "新增AI模型信息")
    public Result<String> add(@Validated @RequestBody RobotAidataInfoInsertVo robotAidataInfoVo) {
        logger.info("新增AI模型信息:{}", robotAidataInfoVo);

        RobotAidataInfo info = robotAidataInfoVo.convert();
        SysTenant sysTenant = sysTenantMapper.selectById(KefuContext.getKefuContext().getTenantId());
        long count = robotAidataInfoService.count(Wrappers.lambdaQuery(RobotAidataInfo.class)
                .eq(RobotAidataInfo::getTenantId, KefuContext.getKefuContext().getTenantId()));
        if (count > sysTenant.getAidataCount()) {
            throw new RuntimeException("已达到最大模型数量！");
        } else {
            robotAidataInfoService.save(info);
        }
        return ResultGenerator.genSuccessResult();
    }


    @PostMapping("/update")
    @ApiOperation(value = "修改AI模型信息")
    public Result<String> update(@Validated @RequestBody RobotAidataInfoUpdateVo robotAidataInfoVo) {
        logger.info("修改AI模型信息:{}", robotAidataInfoVo);

        RobotAidataInfo info = robotAidataInfoVo.convert();
        robotAidataInfoService.updateById(info);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/detail")
    @ApiOperation(value = "AI模型信息详情")
    public Result<RobotAidataInfo> detail(@Validated @RequestBody DetailVo detail) {

        RobotAidataInfo aidataInfo = robotAidataInfoService.getAidataInfo(detail.getId());
        return ResultGenerator.genSuccessResult(aidataInfo);
    }

    @PostMapping("/list")
    @ApiOperation(value = "查询AI模型信息")
    public Result<Page<RobotAidataInfo>> list(@Validated @RequestBody RobotAidataInfoSearchVo search) {

        Page<RobotAidataInfo> page = robotAidataInfoService.searchLambdaPage(search);
        Map<String, String> userMap = sysUserMapper.selectList(Wrappers.lambdaQuery(SysUser.class)
                        .eq(SysUser::getTenantId, KefuContext.getKefuContext().getTenantId()))
                .stream().filter(user -> StringUtils.isNotBlank(user.getUserName()) && StringUtils.isNotBlank(user.getUserId()))
                .collect(Collectors.toMap(SysUser::getUserId, SysUser::getUserName));
        page.getRecords().forEach(info -> {
            info.setCreateUserName(userMap.get(info.getCreateUser()));
            info.setUpdateUserName(userMap.get(info.getUpdateUser()));
        });
        return ResultGenerator.genSuccessResult(page);
    }
}
